package com.example.lenovo_.myfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.example.lenovo_.healthyangel.R;

/**
 * Created by lenovo- on 2016/10/12.
 */
public class ShopFragment extends Fragment{
    private ViewFlipper flipper;
    private int[]resId={R.mipmap.cai1,R.mipmap.cai2,R.mipmap.cai3,R.mipmap.cai4,R.mipmap.cai5,R.mipmap.qicai1,R.mipmap.qicai2,R.mipmap.qicai3,R.mipmap.qicai4};
    private float startX;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_shop,null);
        initView(view);
        for(int i=0;i<resId.length;i++) {
            flipper.addView(getImageview(resId[i]));
        }
        flipper.setInAnimation(this.getContext(),R.anim.slide_right_in);
        flipper.setOutAnimation(this.getContext(),R.anim.slide_right_out);
        flipper.setFlipInterval(6000);
        flipper.startFlipping();
        flipper.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action=event.getAction();
                switch (action){
                    //手指接触屏幕
                    case MotionEvent.ACTION_DOWN:
                        flipper.setAutoStart(false);
                        startX=(int) event.getX();
                        break;
                    //手指栽屏幕上移动
                    case MotionEvent.ACTION_MOVE:
                        break;
                    //手指离开屏幕
                    case MotionEvent.ACTION_UP: {
                        //手指向右滑动，显示下一张图片
                        if (event.getX() - startX > 100) {
                            flipper.setInAnimation(getContext(), R.anim.slide_right_in);
                            flipper.setOutAnimation(getContext(), R.anim.slide_right_out);
                            flipper.showNext();
                            flipper.setAutoStart(false);
                        }
                        //手指向左滑动，显示前一张图片
                        else if (startX - event.getX() > 100) {
                            flipper.setInAnimation(getContext(), R.anim.slide_left_in);
                            flipper.setOutAnimation(getContext(), R.anim.slide_left_out);
                            flipper.showPrevious();
                            flipper.setAutoStart(false);
                        }
                        else if(startX-event.getX()<10||event.getX()-startX<10){
                            break;
                        }
                    }
                    break;
                }
                //手指不滑动时，继续执行自动向右滚动模式
                flipper.setInAnimation(getContext(),R.anim.slide_right_in);
                flipper.setOutAnimation(getContext(),R.anim.slide_right_out);
                flipper.setFlipInterval(6000);
                flipper.setAutoStart(true);
                return true;
            }
        });
        return view;
    }
    private ImageView getImageview(int resId){
        ImageView image = new ImageView(getContext());
        image.setBackgroundResource(resId);
        return image;
    }
    private void initView(View view) {
        flipper= (ViewFlipper) view.findViewById(R.id.flipper2);
    }
}
